package com.health;

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.util.LogTime;
import com.health.model.SanNuoBean;
import com.health.viewscontrols.NovaActivity;
import com.novabluetooth.data.DeviceShowSaveData;
import com.sinocare.multicriteriasdk.MulticriteriaSDKManager;
import com.sinocare.multicriteriasdk.SnCallBack;
import com.sinocare.multicriteriasdk.auth.AuthStatusListener;
import com.sinocare.multicriteriasdk.bean.BaseDetectionData;
import com.sinocare.multicriteriasdk.entity.BoothDeviceConnectState;
import com.sinocare.multicriteriasdk.entity.DeviceDetectionState;
import com.sinocare.multicriteriasdk.entity.SNDevice;
import com.sinocare.multicriteriasdk.utils.AuthStatus;

import java.util.ArrayList;
import java.util.List;

public class TestSanNuoActivity extends NovaActivity {

    private static final String TAG = "TestSanNuoActivity";

    private List<SNDevice> snDevices = new ArrayList<>();
    private TextView tv_state;
    private Button btconn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_san_nuo);

        tv_state = findViewById(R.id.tv_state);
        btconn = findViewById(R.id.btconn);

        SNDevice snDevice = new SNDevice();
        snDevice.setMac("F4:3C:3B:1D:88:20");
        snDevice.setMachineCode("2000");
        snDevice.setProductCode("100015");
        snDevice.setName("三诺臻准2000血糖仪");
        snDevice.setBleNamePrefix("SN-4Y");
        snDevice.setOpenProcessData(false);
        snDevice.setDataProtocolCode(SNDevice.DEVICE_SINO_STANDARD_BLE_01);
        Log.e(TAG, "doBusiness: " + snDevice.toString() );
        snDevices.add(snDevice);

        btconn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startDevice();
            }
        });

        MulticriteriaSDKManager.authentication(new AuthStatusListener() {
            @Override
            public void onAuthStatus(AuthStatus authStatus) {
                Log.e(TAG, "三诺sdk鉴权结果: " + authStatus.toString());
            }
        });

    }

    @Override
    public void findViews() {

    }

    @Override
    public void setListeners() {

    }

    public void startDevice() {
        Log.e(TAG, "startDevice: " );

        // 第一种 默认就是开启扫描设备
        MulticriteriaSDKManager.startConnect(snDevices, new SnCallBack(){
            @Override
            public void onDataComing(SNDevice device, BaseDetectionData data){
                //设备数据回调，解析见后面数据结构，实时测量数据与历史测量数据均在此处回调；
                Log.e(TAG, "onDataComing: " + data.toString() );
                saveData(data);
            }

            @Override
            public void onDetectionStateChange(SNDevice device, DeviceDetectionState state){
                Log.e(TAG, "onDeviceStateChange: -----snDevice----" + device.toString());
                //设备数据状态：时间同步成功、历史数据获取成功、清除成功等等
            }

            @Override
            public void onDeviceStateChange(SNDevice device, BoothDeviceConnectState state){
                Log.e(TAG, "onDeviceStateChange: " + state.toString() );
                //连接连接状态 目前只回调连接成功与断开连接
                if (state != null) {
                    showBleState(state);
                }
            }
        });
    }

    private void saveData(BaseDetectionData data) {
        try {
            if ("04".equals(data.getCode())) {

            } else if ("02".equals(data.getCode())) {
                tv_state.setText("获取测量结果失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            // 断开连接
            MulticriteriaSDKManager.disConectDevice(snDevices);
            snDevices.clear();

            // 退出App
            MulticriteriaSDKManager.finishAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @SuppressLint("WrongConstant")
    private void showBleState(BoothDeviceConnectState state) {
        try {
            Log.e(TAG, "showBleState: " + state.toString() );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
